<?
include_once($_SERVER['DOCUMENT_ROOT']."/lucy/config.php");

class speech{
	public $word;
	public $type;
	public $feeling;
	public $response;
	public $id;

	function speech($feeling = "", $word = "", $type = "", $response = ""){
		$this->word = $word;
		$this->type = $type;
		$this->feeling = $feeling;
		$this->response = $response;
	}
	
	function save_response(){
		if(!$dbcon){include(INC.'dbcon.php');}
		$sql = "INSERT INTO `speech` (`word`, `type`, `feeling`, `response`) VALUES ('{$this->word}', '{$this->type}', '{$this->feeling}', '{$this->response}')";
		$rez = mysql_query($sql, $dbcon);
		return ($rez) ? 1 : 0;
	}	
	
	function save(){
		if(!$dbcon){include(INC.'dbcon.php');}
		$sql = "INSERT INTO `speech` (`word`, `type`, `feeling`, `response`) VALUES ('{$this->word}', '{$this->type}', '{$this->feeling}', '{$this->response}')";
		$rez = mysql_query($sql, $dbcon);
		return ($rez) ? 1 : 0;
	}	
	function update(){
		if(!$dbcon){include(INC.'dbcon.php');}
		$sql = "UPDATE `speech` SET `type` = '{$this->type}', `feeling` = '{$this->feeling}', `response` = '{$this->response}' WHERE `id` = '{$this->id}'";
		$rez = mysql_query($sql, $dbcon);
		return ($rez) ? 1 : 0;
	}
	
	function set_feeling($feeling){
		$this->feeling = $feeling;
	}

	function set_word($word){
		$this->word = $word;
	}

	function set_type($type){
		$this->type = $type;
	}
	function set_response($response){
		$this->response = $response;
	}	
	function set_id($id){
		$this->id = (int)$id;
	}
	
	function get_response_word($type = ""){
		$this->type = ($type != "") ? $type : $this->type;
		
		if(!$dbcon){include(INC.'dbcon.php');}
		$sql = "SELECT `word` FROM `speech` WHERE `type` = '{$this->type}' AND `feeling` = '{$this->feeling}' AND `response` = '1'";
		$rez = mysql_query($sql, $dbcon);
		if(mysql_num_rows($rez)){
			while($wor = mysql_fetch_assoc($rez)){
				$word[] = $wor['word'];
			}
		}
		if(is_array($word)){
			return $word[array_rand($word)];
		}else{
			return 0;
		}
	}	
	
	function get_feeling($word = ""){
		$this->word = ($word != "") ? $word : $this->word;
		
		if(!$dbcon){include(INC.'dbcon.php');}
		$sql = "SELECT `feeling` FROM `speech` WHERE `word` = '{$this->word}' AND `response` = '0'";
		$rez = mysql_query($sql, $dbcon);
		if(mysql_num_rows($rez)){
			while($feel = mysql_fetch_assoc($rez)){
				$feeling[] = $feel['feeling'];
			}
		}
		if(is_array($feeling)){
			return $feeling[array_rand($feeling)];
		}else{
			return 0;
		}
	}
	
	function get_type($word = "", $like = 0){
		$this->word = ($word != "") ? $word : $this->word;
		
		if(!$dbcon){include(INC.'dbcon.php');}
		$like = ($like == 0) ? "= '{$this->word}'" : "LIKE '%{$this->word}%'";
		$sql = "SELECT `type` FROM `speech` WHERE `word` $like AND `response` = '0'";
		$rez = mysql_query($sql, $dbcon);
		if(mysql_num_rows($rez)){
			while($typ = mysql_fetch_assoc($rez)){
				$type[] = $typ['type'];
			}
		}
		if(is_array($type)){
			return $type[array_rand($type)];
		}else{
			return 0;
		}
	}	
	
	function think_type($word = ""){
		$this->word = ($word != "") ? $word : $this->word;
		$w = explode("+", $this->word);
		foreach($w AS $name){
			$t[$this->get_type($name, 1)]++;
		}
		//print_r($t);
		$tmp = 0;
		foreach($t AS $key => $val){
			if($val > $tmp AND $key != ""){
				$ret = $key;
			}
		}
		return ($ret != "") ? $ret : 0;
	}
	
	function get_id($word = ""){
		$this->word = ($word != "") ? $word : $this->word;
		if(!$dbcon){include(INC.'dbcon.php');}
		$sql = "SELECT `id` FROM `speech` WHERE `word` = '{$this->word}' AND `response` = '0'";
		$id = mysql_fetch_assoc(mysql_query($sql, $dbcon));
		return $id['id'];
	}
	
	function show(){
		echo "<br/>-------------------------";
		echo "<br/>".$this->id;
		echo "<br/>".$this->word;
		echo "<br/>".$this->type;
		echo "<br/>".$this->feeling;
		echo "<br/>-------------------------";
	}
}